<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
         <acc-set-card></acc-set-card>
        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class="handle-box flex flex-center">
                <el-button type="primary" icon="el-icon-folder" @click="saveEdit()">保存</el-button>
            </div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px">
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="单别名称" prop="ruleName">
                            <el-input v-model="ruleForm.ruleName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="系统模块" prop="systemModel">
                            <el-select
                                v-model="ruleForm.systemModel"
                                placeholder="请选择"
                                clearable
                                @focus="getMoList"
                            >
                                <el-option
                                    :label="item"
                                    :value="item"
                                    v-for="(item, index) in moList"
                                    :key="index"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="作业" prop="task">
                            <el-select
                                v-model="ruleForm.task"
                                placeholder="请选择"
                                clearable
                                @focus="getTaskList"
                            >
                                <el-option
                                    :label="item"
                                    :value="item"
                                    v-for="(item, index) in taskList"
                                    :key="index"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6"></el-col>
                </el-row>
            </el-form>
        </div>
    </div>
</template>


<script>
import appMain from '../../../utils/app_main';
import _ from 'lodash';
import XEUtils from 'xe-utils';

export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            //模块
            moList: [],
            //作业
            taskList: [],
            rules: {
                ruleName: [
                    { required: true, message: '请输入单别', trigger: 'change' },
                    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'change' }
                ],
                systemModel: [{ required: true, message: '请选择', trigger: 'change' }],
                task: [{ required: true, message: '请选择', trigger: 'change' }]
            },
            ruleForm: {
                ruleCode: '', //单别代码
                ruleName: '', //单别名称
                systemModel: '', //系统模块
                task: '' //作业
            }, //新增model
            editForm: {},
            isUpdate: false
        };
    },
    created() {
        if (this.$route.query.id) {
            let id = this.$route.query.id;
            //是更新操作
            this.isUpdate = true;
            //根据id获取单别
            this.getDataById(id);
        } else {
            //新增操作
            this.isUpdate = false;
        }
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 200;
        this.pageTitle = this.$router.currentRoute.meta.title;
        //$refs.xTree.setAllTreeExpand(true);
    },
    methods: {
        getDataById(id) {
            appMain
                .orderNoRuleFindById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.ruleForm = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        addRate() {
            appMain
                .orderNoRuleInsert(this.ruleForm)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.$router.push({
                            path: '/singleSet' //跳转的路径
                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        updateRate() {
            appMain
                .orderNoRuleUpdateById(this.ruleForm)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.$message.success(res.data.message);
                        this.$router.push({
                            path: '/singleSet' //跳转的路径
                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        getMoList() {
            appMain
                .orderNoRuleFindModelOfNo()
                .then(res => {
                    if (res.data.code === 20000) {
                        this.moList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        getTaskList() {
            appMain
                .orderNoRuleFindTask()
                .then(res => {
                    if (res.data.code === 20000) {
                        this.taskList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        submitForm(formName) {
            this.$refs['ruleForm'].validate(valid => {
                if (valid) {
                    if (formName == 'ruleForm') {
                        this.addRate();
                    } else if (formName == 'editForm') {
                        this.updateRate();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        saveEdit() {
            if (this.isUpdate == false) {
                this.submitForm('ruleForm');
            } else if (this.isUpdate == true) {
                this.submitForm('editForm');
            }
        }
    },
    computed: {},
    watch: {}
};
</script>

<style lang="css" scoped>
.el-card {
    height: 100%;
}

.handle-box {
    padding: 5px 50px;
    width: 100%;
    background-color: #fff;
    position: absolute;
    left: 0;
    bottom: 0px;
    border-bottom: 1px solid #ddd;
    border-top: 1px solid #ddd;
    z-index: 1;
}

.el-input {
    max-width: 215px;
}
</style>